jQuery(function() {
	var loading = false;
	
	var uploadFormOptions = {
		url : ctx+'/fileupload/file',
		type : 'post',
		dataType : 'xml',		
		beforeSubmit : function(formData, jqForm, options){
			var form = jqForm[0];
			if (form.image.value) {
				jQuery("#txtFileName").iBlock({img: ctx+'/images/ajaxloading.gif'})
				return true;
			}
			
			iAlert("Bạn chưa chọn hình ảnh để upload");
			return false;
		},
		success : function(responseXML){
			var image = jQuery('image', responseXML).text();
			window.location = ctx+"/editAvatar.html?image="+image;
			loading = false;
			jQuery("#txtFileName").iUnblock();
		}
	};
	
	jQuery("a#uploadAvatarSubmitButton").click(function(){
		if(loading == true){
			return false;
		}
		
		loading = true;
		jQuery("form#uploadAvatarForm").submit();		
		return false;
	});
			
	jQuery("form#uploadAvatarForm").ajaxForm(uploadFormOptions);
	
	jQuery("#showAvatarBox").fancybox();
	jQuery("#showAvatarBoxLink").fancybox();
	
	jQuery("#avatarFile").change(function(){
		jQuery("#txtFileName").val(jQuery(this).val());
	});
});